package cn.cy.service.imp;

import cn.cy.domain.Const.LowerConst;
import cn.cy.domain.Const.WebsocketConst;
import cn.cy.domain.DTO.ResultDTO;
import cn.cy.domain.StepResult;
import cn.cy.service.WsLowerCom;
import cn.cy.websocket.WebSocketServer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.RandomUtil;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author: Xucranger
 * @Date: 22/5/5 2:21
 */

@Slf4j
@Service
public class WsLoweComImp implements WsLowerCom {
    //下位机sid
    public static final String lowersid= WebsocketConst.Lowersid;
    //前端sid
    public static final String frontsid= WebsocketConst.Frontsid;
    //格子移动的距离
    public static final Integer xunit = LowerConst.xunit;
    public static final Integer yunit = LowerConst.yunit;

    public static final ArrayList<String> missionlist = new ArrayList<String>();



    /**
     * @author: Xucranger
     * @description: 发送指令，id是电机名称，step是绝对步数
     * @date: 22/5/5 2:23
     */
    @Async("sendcmd")
    public  void sendcmd(Integer id, Integer step) throws IOException {
        String logstr = "move motor"+id.toString()+" to "+step.toString();
        //生成一个随机数，用来标记任务
        String cmdid = RandomUtil.randomNumbers(10);
        log.info("任务id:"+cmdid+"=="+logstr);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cmdid",cmdid);
        jsonObject.put("id",id);
        jsonObject.put("step",step);
        String data = JSON.toJSONString(jsonObject);

        //向下位机发送指令
        WebSocketServer.sendMsg(data,lowersid);
    }

    /**
     * @author: Xucranger
     * @description: 初始化机器位置
     * @date: 22/5/1 16:00
     * @return result
     */
    @Override
    public void init() throws IOException {
        //xyz三轴归位
        sendcmd(3,0);
        sendcmd(1,0);
        sendcmd(2,0);

    }


    /**
     * @author: Xucranger
     * @description: 移动抽屉
     * @date: 22/5/5 16:08
     * @return result
     */
    @Override
    public void movedrawer(Integer step) throws IOException {
        //a轴移动
        sendcmd(4,step);
    }

    /**
     * @author: Xucranger
     * @description: 到达格子位置
     * @date: 22/5/1 16:11
     * @param x,y
     * @return result
     */
    @Override
    public void arriveto(Integer x, Integer y) throws IOException {
        Integer xstep = x*xunit;
        Integer ystep = y&yunit;
        sendcmd(1,xstep);
        sendcmd(2,ystep);
    }

    /**
     * @author: Xucranger
     * @description: 返回结果
     * @date: 22/5/5 2:50
     */
    @Override
    public void getResult(String status) throws IOException {
        log.info(status+"完成任务");

        StepResult stepResult = StepResult.succ(status);
        ResultDTO resultDTO = new ResultDTO("success",stepResult);
        //向前端发送返回结果
        WebSocketServer.sendMsg(resultDTO.toString(),frontsid);

        //status--动作
        //init--返回初始化
        //popdrawer--返回抽屉推出
        //pushdrawer--返回抽屉推入
        //arriveto--返回印章到达
        //stamp--盖章一瞬间完成,进行注册
        if(status.equals("stamp")){
            //这里写注册的内容
        }
        //popdrawer--返回抽屉推出

    }
}
